import random

import pygame


class BaseProp(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()


class BoomProp(BaseProp):
    """
    炸弹道具，作用是直接消灭所有正在活动的敌方坦克
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/boom.png").convert_alpha()
        self.rect = self.image.get_rect()


class PauseProp(BaseProp):
    """
    暂停道具，作用是将所有坦克暂停 30 秒
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/pause.png").convert_alpha()
        self.rect = self.image.get_rect()


class ReinforceProp(BaseProp):
    """
    加固道具，作用是将玩家基地的围墙使用钢铁加固
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/reinforce.png").convert_alpha()
        self.rect = self.image.get_rect()


class ShieldProp(BaseProp):
    """
    护盾道具，作用是对使用该道具的玩家施加一层 30 秒的护盾，使其免疫被敌方坦克摧毁
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/shield.png").convert_alpha()
        self.rect = self.image.get_rect()


class HealthProp(BaseProp):
    """
    生命加一道具，作用将使用该道具的玩家生命数加一
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/health.png").convert_alpha()
        self.rect = self.image.get_rect()


class UpgradeProp(BaseProp):
    """
    升级道具，作用是让使用该道具的玩家升一级
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/upgrade.png").convert_alpha()
        self.rect = self.image.get_rect()


class UltimateUpgradeProp(BaseProp):
    """
    终极升级道具，作用是让使用该道具的玩家直接升到满级
    """

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r"./assets/images/props/ultimate_upgrade.png").convert_alpha()
        self.rect = self.image.get_rect()


class PropFactory:

    def __init__(self):
        # 各种道具出现概率百分比
        self.prop_probabilities = [
            (BoomProp, 20),
            (PauseProp, 20),
            (ShieldProp, 20),
            (ReinforceProp, 15),
            (UpgradeProp, 12),
            (HealthProp, 7),
            (UltimateUpgradeProp, 6)
        ]
        self.props = []
        self.probabilities = []
        for prop_cls, prob in self.prop_probabilities:
            self.props.append(prop_cls)
            self.probabilities.append(prob)

        # 生成道具的声音
        self.generate_prop_sound = pygame.mixer.Sound("assets/sounds/generate_prop.ogg")

    def generate_prop(self, prop_forbidden_landscape_group=None, player_tank_group=None):
        self.generate_prop_sound.play()
        prop_cls = random.choices(self.props, weights=self.probabilities, k=1)[0]
        prop_obj = prop_cls()
        prop_obj.rect.left = random.randint(1, 12) * 48
        prop_obj.rect.top = random.randint(1, 12) * 48
        if pygame.sprite.spritecollide(prop_obj, prop_forbidden_landscape_group, False) or \
            pygame.sprite.spritecollide(prop_obj, player_tank_group, False):
            prop_obj.rect.left = random.randint(1, 12) * 48
            prop_obj.rect.top = random.randint(1, 12) * 48
        return prop_obj
